package com.ruipengkj.component.jlist;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.TexturePaint;

import javax.swing.Icon;
import javax.swing.JButton;

import com.ruipengkj.commons.util.Util;
import com.ruipengkj.factory.ImageFactory;

public class ListNodeButton extends JButton {
	
	private static final long serialVersionUID = 1L;
	
	private Image backgroundselectImage = ImageFactory.loadImage(ImageFactory.LIST_NODE_BTN_BG).getImage();
	private TexturePaint paintSelect = Util.createTexturePaint(backgroundselectImage);
	private Icon selectIcon = ImageFactory.loadImage(ImageFactory.LIST_NODE_BTN_SELECT_ICON);
	private Icon icon = ImageFactory.loadImage(ImageFactory.LIST_NODE_BTN_ICON);
	private Image rightImage = ImageFactory.loadImage(ImageFactory.LIST_NODE_BTN_IMAGE_RIGHT).getImage();
	
	
	public ListNodeButton(String text) {
		super(" "+text);
		this.setBorder(null);
		setContentAreaFilled(false); // 这一句非常重要, 否则父类还会绘制按钮的区域. 
		this.setHorizontalAlignment(JButton.LEFT);
		this.setIcon(icon);
		this.setSelectedIcon(selectIcon);
		Util.setSize(this, 178, 33);
		this.setOpaque(false);
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		this.setOpaque(false);
		Graphics2D g2d = (Graphics2D) g;
		if (getModel().isSelected()) {
			g2d.setPaint(paintSelect);
			g2d.fillRect(0, 0, getWidth(), getHeight());
			g2d.drawImage(rightImage, 153, 14, null);
			this.setForeground(Color.white);
		} else {
			this.setForeground(Color.black);
//			g2d.setPaint(paint);
//			g2d.fillRect(0, 0, getWidth(), getHeight());
//			int x = 4;
//			int y = getHeight()-1;
//			int width = getWidth()-5;
//			while (x < width) {
//				g2d.drawLine(x, y, x, y);
//				x += 2;
//			}
		}
		super.paintComponent(g);
	}
}
